package shao.domain;

import lombok.Getter;

public class GeneralObjectResp<T> {
    private static final String SUCCESS_CODE = "1";
    private static final String SUCCESS_DESC = "success";
    private static final String FAILURE_CODE = "2";

    @Getter
    private String bizCode;
    @Getter
    private String bizDesc;
    @Getter
    private T item;

    public static <T> GeneralObjectResp<T> success(T item) {
        return GeneralObjectResp.build(SUCCESS_CODE, SUCCESS_DESC, item);
    }

    public static <T> GeneralObjectResp<T> failure(String format, Object... args) {
        return GeneralObjectResp.buildWithCode(FAILURE_CODE, format, args);
    }

    public static <T> GeneralObjectResp<T> buildWithCode(String code, String format, Object... args) {
        return GeneralObjectResp.build(code, String.format(format, args), null);
    }

    public boolean isSuccess() {
        return SUCCESS_CODE.equals(bizCode);
    }

    private static <T> GeneralObjectResp<T> build(String code, String desc, T item) {
        GeneralObjectResp<T> obj = new GeneralObjectResp<>();
        obj.bizCode = code;
        obj.bizDesc = desc;
        obj.item = item;
        return obj;
    }

}
